package com.usefullc.demo.service;

import com.usefullc.demo.dao.DemoDao;
import com.usefullc.demo.domain.Demo;
import com.usefullc.demo.query.DemoQuery;
import com.usefullc.system.service.AbstractBizService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author: Sampson
 * @date: 2018-12-19 16:05
 */
@Service
public class DemoService extends AbstractBizService<Demo, DemoQuery> {

    @Autowired
    private DemoDao demoDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @Override
    public List<Predicate> getPredicates(Root<Demo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder, DemoQuery thisQuery) {
        List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.isNotEmpty(thisQuery.getGmtCreateStart())) {
            Expression<String> endTimeExpression = criteriaBuilder.function("DATE_FORMAT", String.class,
                    root.get("gmtCreate"), criteriaBuilder.literal("%Y-%m-%d"));
            predicates.add(criteriaBuilder.greaterThanOrEqualTo(endTimeExpression, thisQuery.getGmtCreateStart()));
        }
        if (StringUtils.isNotEmpty(thisQuery.getGmtCreateEnd())) {
            Expression<String> endTimeExpression = criteriaBuilder.function("DATE_FORMAT", String.class,
                    root.get("gmtCreate"), criteriaBuilder.literal("%Y-%m-%d"));
            predicates.add(criteriaBuilder.lessThanOrEqualTo(endTimeExpression, thisQuery.getGmtCreateEnd()));
        }
        return predicates;
    }




}
